import {api} from './libs/api.js';
import {member} from '@/api/member.js';
let _self;
export const index =  {
	data() {
		return {
			optLock:false,
			showAgree:false,
			salerShow:false,
			salerInfo:{},
			agreeChecked:"否",
			sex:api.sex,
			agree:api.agree,
			rules:api.rules,
			formData:api.formData,
			customerKind:{
				dataList:["周边小店","工厂","经销商"],
				index: 0
			},
			formField:[],
		}
	},
	
	watch:{
		salerInfo(val){
			this.formData["salesman"]=val["user_name"];
			this.formData["salesman_code"]=val["user_code"];
		}
	},
	mounted() {
		_self = this;
	},
	onLoad:function(option) {
		this.formData["id"]=option.id?option.id:0;
	},
	methods: {
		
		async initData(){
			this.formField=await api.getFormField({});
			this.formData["customer_kind"]=this.customerKind.dataList[this.customerKind.index];
			if(this.formData["id"]){
				this.formData = await api.getCustomer({"id":this.formData["id"]});
				this.customerKind.index=this.customerKind.dataList.indexOf(this.formData["customer_kind"]);
			}else{
				this.formData["salesman"]=this.member["user_name"];
				this.formData["salesman_code"]=this.member["user_code"];
			}
			
			
		},
		
		selectCustomerKind(e){
			this.customerKind.index = e.detail.value
			this.formData["customer_kind"]=this.customerKind.dataList[this.customerKind.index];
		},
		
		
		async submitForm(form){
			if(_self.optLock) return true;
			_self.optLock=true;
			
			
			if(this.agreeChecked!="是"){
				uni.showToast({icon:"none",title:"请勾选《用户隐私保护协议》！"});
				_self.optLock=false;
				return true;
			}
			
			
			this.$refs.form.validate().then(res=>{
				_self.addCustomer();
			}).catch(err =>{
				_self.optLock=false;
			})
		},
		
		async addCustomer(res){

			let ret= await api.addCustomer(this.formData);
			if(ret=="success"){
				uni.showToast({icon:"none",title:"添加成功！"});
				setTimeout(function(){uni.navigateTo({url:"/packagePages/pages/customer/index"});},2000);　　
			}else{
				_self.optLock=false;
			}

		},
		
		selectSales(){
			uni.hideKeyboard();
			this.salerShow=true;
		},
		
		agreeClick(res){
			this.showAgree=true;
		}
		
	},
}